package org.ft.fileConvert.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.ft.fileConvert.entity.FileConvert;
import org.ft.fileConvert.service.AttachService;
import org.ft.fileConvert.service.FileConvertService;
import org.ft.fileConvert.vo.ResultVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chentianzhong
 * @since 2022-08-10
 */
@Api(tags = "文件管理")
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileConvertService fileConvertService;
    @Resource
    private AttachService attachService;

    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")
    @PostMapping("upload")
    public ResultVo upload(@RequestParam("file") MultipartFile file, @RequestParam("convertType") Integer convertType) throws IOException {
        if (file == null){
            return ResultVo.fail(500,"文件不能爲空");
        }
        return ResultVo.success(fileConvertService.upload(file, convertType));
    }

    @ApiOperation(value = "删除文件", notes = "", httpMethod = "DELETE")
    @DeleteMapping("delete/{id}")
    public ResultVo delete(@PathVariable("id") String id) throws IOException {
        fileConvertService.removeById(id);
        return ResultVo.success();
    }

    @ApiOperation(value = "下载附件", notes = "下载转化好的文件对象", httpMethod = "GET")
    @GetMapping("download/{id}")
    public void download(@PathVariable("id") String id, HttpServletResponse response) throws IOException {
        FileConvert fileConvert = fileConvertService.getById(id);
        if(fileConvert !=null && StringUtils.hasText(fileConvert.getFinalPath())){
            InputStream inputStream = new FileInputStream(fileConvert.getFinalPath());
            response.reset();
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileConvert.getFinalName(), "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = inputStream.read(b)) > 0) {
                outputStream.write(b, 0, len);
            }
            inputStream.close();
        }

    }


    @ApiOperation(value = "添加附件", notes = "向ofd文件中添加附件", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ofdUrl", value = "原始ofd文件下载地址", required = true,paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "attachmentFileUrl", value = "要添加的附件文件下载地址", required = true,paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "attachmentName", value = "要添加的附件文件显示名称", required = true,paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "ext", value = "要添加的附件文件扩展名，当url中没有文件扩展名时需要,如.jpg", required = true,paramType = "query", dataType = "String")
    })
    @PostMapping("/attachment/add")
    public ResultVo addOfdAttachment(@RequestParam String ofdUrl,
                                     @RequestParam String attachmentFileUrl,
                                     @RequestParam String attachmentName,
                                     @RequestParam(required = false) String ext) {
        return attachService.addOfdAttachment(ofdUrl, attachmentFileUrl, attachmentName, ext);
    }

}
